package cn.edu.zafu.bigdata.lmr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

class Student2 {
    private String id;
    private String name;
    private String phone;

    Student2(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}



public class StudentManagementSystem2 {
    private static final String URL = "jdbc:mysql://192.168.169.76:3306/learnjava?useSSL=false&serverTimezone=UTC";
    private static final String USER = "learn";
    private static final String PASSWORD = "learnpassword";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // 确保驱动被加载
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    running = false;
                    break;
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        }

        scanner.close();
    }

    private static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try (Connection conn = getConnection()) {
            String checkQuery = "SELECT * FROM students WHERE id = ?";
            try (PreparedStatement checkStmt = conn.prepareStatement(checkQuery)) {
                checkStmt.setString(1, id);
                ResultSet rs = checkStmt.executeQuery();
                if (rs.next()) {
                    System.out.println("学号已存在，请重新输入。");
                    return;
                }
            }

            String insertQuery = "INSERT INTO students (id, name, phone) VALUES (?, ?, ?)";
            try (PreparedStatement insertStmt = conn.prepareStatement(insertQuery)) {
                insertStmt.setString(1, id);
                insertStmt.setString(2, name);
                insertStmt.setString(3, phone);
                insertStmt.executeUpdate();
                System.out.println("学生信息添加成功。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号 (输入 QUIT 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            try (Connection conn = getConnection()) {
                String query = "SELECT * FROM students WHERE id = ?";
                try (PreparedStatement stmt = conn.prepareStatement(query)) {
                    stmt.setString(1, id);
                    ResultSet rs = stmt.executeQuery();
                    if (rs.next()) {
                        System.out.println("学号: " + rs.getString("id") + ", 姓名: " + rs.getString("name") + ", 电话: " + rs.getString("phone"));
                    } else {
                        System.out.println("未找到该学生。");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号 (输入 QUIT 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            System.out.print("请输入新姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新电话: ");
            String phone = scanner.nextLine();

            try (Connection conn = getConnection()) {
                String updateQuery = "UPDATE students SET name = ?, phone = ? WHERE id = ?";
                try (PreparedStatement updateStmt = conn.prepareStatement(updateQuery)) {
                    updateStmt.setString(1, name);
                    updateStmt.setString(2, phone);
                    updateStmt.setString(3, id);
                    int rowsAffected = updateStmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("学生信息更新成功。");
                    } else {
                        System.out.println("未找到该学生。");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号 (输入 QUIT 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            try (Connection conn = getConnection()) {
                String deleteQuery = "DELETE FROM students WHERE id = ?";
                try (PreparedStatement deleteStmt = conn.prepareStatement(deleteQuery)) {
                    deleteStmt.setString(1, id);
                    int rowsAffected = deleteStmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("学生信息删除成功。");
                    } else {
                        System.out.println("未找到该学生。");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}